const { orderDetail } = require("../../utils/api");

// pages/orderpage/index.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    address: {},
    orderinfo: {},
    showSure: false,//确认弹窗
    showCancel: false,//取消订单弹窗
    goodsId: '',
    addressdata: null,
    myData: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

    this.setData({
      goodsId: options.goodsid
    })


  },

  onShow() {
    var pages = getCurrentPages();
    var currPage = pages[pages.length - 1];
    this.data.myData = currPage.data.preData
    if (this.data.myData) {
      this.setData({
        address: this.data.myData
      })
    } else {
      this.getOrderDetail()
    }


  },

  async getOrderDetail() {
    let params = {
      gid: this.data.goodsId
    }
    await orderDetail(params).then((res) => {
      if (res.errcode == 0) {
        //地址
        let resAdd = res.data.addressdata
        this.setData({
          addressdata: resAdd
        })

        if (this.data.addressdata != null) {
          this.setData({
            address: resAdd,
            "address.address": `${resAdd.province}${resAdd.city}${resAdd.area}${resAdd.address}`,
          })
        }


        //订单
        let resOrder = res.data.orderinfo

        this.setData({
          orderinfo: resOrder
        })
      } else {
        wx.showToast({
          title: res.errmsg,
          icon: 'none',
        });

      }
    })
  },

  toAddress() {
    wx.navigateTo({
      url: `/packageA/pages/editaddress/index`,

    });

  },


  changeAddress() {
    wx.navigateTo({
      url: '/packageA/pages/addresspage/index?urltype=change',

    });

  },

  //确认兑换
  onSureChange() {
    this.setData({
      showSure: true
    })
  },

  onCancelOrder() {
    this.setData({
      showCancel: true
    })
  },

  onBack() {
    wx.navigateBack({
      delta: 1
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },



})